//
//  HeadView.m
//  212--Homework  QQy音乐  阿里星球
//
//  Created by 冀永金 on 16/8/12.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "HeadView.h"
#import "CycleCollectionCell.h"
#import "FirstModel.h"
#import "SecondModel.h"
#import "ThirdModel.h"
#import "JYYNetWorkParser.h"
#import "UIImageView+WebCache.h"
#import "SmallCollectionViewCell.h"
#define WIDTH  self.bounds.size.width
#define HEIGHT   self.bounds.size.height
@interface HeadView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *cycleCollection;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) FirstModel *first;
@property (nonatomic , strong) UIPageControl *myPage;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UICollectionView *smallCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layoutSmall;
@end
@implementation HeadView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCycleView];
        [self createPageController];
        [self createSmalleCollectionView];
        [self handleDataCycle];
        [self addTimer];
        
    }
    return self;
}
- (void)handleDataCycle {
    [JYYNetWorkParser GETWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400" par:nil success:^(id responseObject) {
        self.first = [FirstModel modelWithDic:responseObject];
        [self.cycleCollection reloadData];
        [self.smallCollectionView reloadData];
        [self.cycleCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    } filed:^(NSError *error) {
        
    }];

}
// 轮播图
- (void)createCycleView {
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.minimumLineSpacing = 0;
    self.cycleCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200) collectionViewLayout:_layout];
    _cycleCollection.backgroundColor = [UIColor whiteColor];
    _cycleCollection.delegate = self;
    _cycleCollection.dataSource = self;
    [self addSubview:_cycleCollection];
    [_cycleCollection registerClass:[CycleCollectionCell class] forCellWithReuseIdentifier:@"poolOfCycle"];
    _cycleCollection.pagingEnabled = YES;
    _cycleCollection.showsHorizontalScrollIndicator= NO;
    
}
// 轮播图下图片
- (void)createSmalleCollectionView {
    _layoutSmall = [[UICollectionViewFlowLayout alloc]init];
    _layoutSmall.minimumLineSpacing = 1;
    _layoutSmall.minimumInteritemSpacing = 1;
    _layoutSmall.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _smallCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 200, WIDTH, 100) collectionViewLayout:_layoutSmall];
    [self addSubview:_smallCollectionView];
    _smallCollectionView.delegate = self;
    _smallCollectionView.dataSource =self;
    [_smallCollectionView registerClass:[SmallCollectionViewCell class] forCellWithReuseIdentifier:@"poolOfSmall"];
    _smallCollectionView.showsHorizontalScrollIndicator= NO;
    _smallCollectionView.backgroundColor =[UIColor colorWithRed:0.07 green:0.10 blue:0.12 alpha:1.00] ;
    _smallCollectionView.scrollEnabled = NO;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.layout.itemSize = CGSizeMake(WIDTH, 200);
    self.cycleCollection.frame = CGRectMake(0, 0, WIDTH, 200);
    self.myPage.frame = CGRectMake(0, 150, WIDTH, 50);
    self.layoutSmall.itemSize = CGSizeMake(WIDTH / 3, 100);
    self.smallCollectionView.frame = CGRectMake(0, 200, WIDTH, 100);
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (collectionView == _cycleCollection) {
        return 100;
    }else {
        
        return 1;
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == _cycleCollection) {
        self.myPage.numberOfPages = self.first.data[0].data.count;
        return self.first.data[0].data.count;
        
    }else {
        return self.first.data[1].data.count;
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.cycleCollection) {
        CycleCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfCycle" forIndexPath:indexPath];
        ThirdModel *third = self.first.data[0].data[indexPath.row];
        [cell setThird:third];
        return cell;
        
    }else {
        SmallCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfSmall" forIndexPath:indexPath];
       // cell.backgroundColor = [UIColor redColor];
        ThirdModel *third = self.first.data[1].data[indexPath.row];
        [cell setThird:third];
        return cell;
    }
}
// page
- (void)createPageController {
    _myPage = [[UIPageControl alloc]init];
    [self addSubview:_myPage];
    //_myPage.backgroundColor = [UIColor yellowColor];
    
}
// page算法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    int page = (int) (scrollView.contentOffset.x / WIDTH + .5) % self.first.data[0].data.count;
    self.myPage.currentPage = page;
}
- (void)addTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
- (void)nextImage {
    // 设置当前的indexPath
    NSIndexPath *curretnindexPath = [[self.cycleCollection indexPathsForVisibleItems]lastObject];
    //NSLog(@"%ld",curretnindexPath.section);
    // 重新设置indexPath
    NSIndexPath *currentIndexPathRest = [NSIndexPath indexPathForItem:curretnindexPath.item inSection:50];
    [self.cycleCollection scrollToItemAtIndexPath:currentIndexPathRest atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    // 设置下一个滚动的Item
    NSInteger nextItem = currentIndexPathRest.item + 1;
    NSInteger nextSection = currentIndexPathRest.section;
    if (nextItem == self.first.data[0].data.count) {
        nextItem = 0;
        nextSection ++;
    }
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    [self.cycleCollection scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
- (void)removeTimer {
    [self.timer invalidate];
    self.timer = nil;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self addTimer];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
